﻿using System.Collections.Generic;
using System.IO;
using System.Windows.Media.Imaging;
using ICSharpCode.SharpZipLib.Zip;

namespace ComicReader.Model
{
    public class ZippedPageRepository : IPageRepository
    {
        public ZippedPageRepository() {}

        public ZippedPageRepository(FileInfo fileInfo)
        {
            Load(fileInfo);
        }

        private ZipFile ZipFile;

        public List<PageData> PageData
        {
            get;
            set;
        }


        public void Load(FileInfo fileInfo)
        {
            ZipFile = new ZipFile(fileInfo.FullName);

            PageData = new List<PageData>();

            if (fileInfo.FullName.IsImage()) return;

            foreach (ZipEntry zipentry in ZipFile)
            {
                if (!zipentry.IsFile) continue;
                if (!zipentry.CanDecompress) continue;

                PageData.Add(new PageData
                                 {
                                     Number = zipentry.ZipFileIndex,
                                     Image = GetBitmapImage(zipentry)
                                 });
            }

        }

 

        private BitmapImage GetBitmapImage(ZipEntry zipEntry)
        {
            return ZipFile.GetInputStream(zipEntry).GetBitmapImage();
        }
    }
}
